package service

import (
	"context"
	"engine/model"
	"fmt"
	"github.com/smartwalle/alipay/v3"
	"lib/db"
	sferrors "lib/sf-errors"
)

func GetAliOpenid(authCode string, user model.My_user) error {

	appID := "2021005114691079"
	privateKey := "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8ol9RAQSO/Ee9eSXqU1iaos57YHMd951UEOYychwvSdQaQbTF1I6S5zvu5Jy1jksMlnb8sbLkQjC+afns3ln9EyQZLaVa6s+K2bZ7Y/s7AboeJmBP2opBuOL5Su9KWzMcfB5BsVrueASvpkxX6RhS/ohivnLOFmhEJVCjeCsb2r4BSAOXU4u954C+lVHrAw84mpw8CiLHHrnaZcT/gyOILAg8feN50qXPqoTF+lXWtnM5VNpvynoUfVoXZU9ECLk0X0JqKjfOUDSODwV8C0GFDSUkIfm/y3q6orZoWpJoBw9izUfBCIKOnjP0EBpm302fGjce/Ii0NoInnqU72HYLAgMBAAECggEADnmTQN8IH4TCaIq0Wx/cK7mUiKklDEak/v9w+qwA96v9p6olGg0uh51Q3wsR72ik36loJgnoxVVC8kmlkQSa+S2P1pLHHovoMypzs1I/xA5k3s1pFJebhmGOoTyb0iPfby8jDz1x0dzJ10S2lQRhBnVbt+APZTsY8ym9311oSXHErQyU3GLkB6jcFztuXrQE/cy6boXDFjYLMHK9EdevfWUiiglc3T3/mS/k/4soV6l2ZATFGf/QSzdHDHPcVUA8/nRgoHfgVw2CyT9uy4+cDimyHjZsKrZGhUN+eYF6GPGaA7I5jafwd+X7b6RQZTve1ZQpJuraWHHcnqVBiPMtQQKBgQD3spxknY0zA+WwcmENl67JEzacIRj6TNnIrfyItt8zbop11NGTSz+kOWgVK2L1ZNDWgLb78GzVwZhImrNYCdtRyoU+x3Ov58tLA1s5cQXcQ3ed0V2AyhOD81RJxx62A3BKL3hmaPZv6EYTZPht1nhM3f35gGJYn/kIyHx4QIYIKwKBgQDC9PaOsBPNbFUBNmPvGqcCgLpfvhT2y8LcLPmGKvmyCwpjXDl6JcZTiL6iVWdhHajUSN9gny+B3BrcdUSIC6wX6ngRvns8kKaEghaZ2e2kfdEst68fxJKwmraJskoVYoRlVOqhG7kv05NdAIVfNjyLf3GFFflDtpCZKUhYV5N5oQKBgQCOfrwlzdVbEdTjSwm2ThqJJnp2GzxJUki/gAgVhk6lQeR5oPLn75QvH69W7lzBmzvhCq86BLg3yGWY7xZZgWIgjYdV9FwDSC/VeZrTUsfyTQXVToqh+p57fFJzFw/92cCRjPmQwLV074TQx3d7iLQwVE01abwqC+2yNdQVQdlZfQKBgHTZPa3n+vZ0p75CS0NURiVJ4mrgEtCY8AHnMucneuErnN5QuGJtoaEPpLLHXV7n1/jkdH4E6wR6V98qqhigZlUvQyffcx2p1SQ2lDVCWRzuDCExNtVjjEgtcpq1MZH/qjrR2o8VfrkbpodneIjIiSFTEUcjFHNxSNmD1MieNNYBAoGAN205UpLBTZkm+fEx2Y6KikBH5aHcuVSQj/TsD+7qrZIfAMPmQPpBFGqpAx+dtG52THNyWUlVJ9N62cHV1HL65uNd2PTO3k5Rz4j5ib9XwFZtl2O4ESSy9oQN/RlseygSxJ3OSx8Pc4RIKlExjfjyripwx126M2ig/OsCt2K9u94="
	var client, err = alipay.New(appID, privateKey, true)
	if err != nil {
		fmt.Println(err)
		return err
	}

	param := alipay.SystemOauthToken{
		Code:      authCode,
		GrantType: "authorization_code",
	}
	client.LoadAliPayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjDdrjze7ztRp7Xv6OmOlkFSnPUoxGzi+bzRu1e90k8iACe2lZUFyLRRKoXmkgdulNDm8H2KKZPfj7y+48EkA99dHUeqFeUghX/geKOCEYxm6dZcJZlV/veCdVL3soCloTDqXeczdZrSr24vRVzVhzRyn7+dCJ9OBJiMqjZ8oN5/BnBqO+os8qn1eX8julFDc0K8FDJqqSEzNF9Ip8JzBFzdyluMq0eVjcECTxHuyPeDzZ7gzeqkk7ritY1mCg6nFVlwL6fBDq6Yl7NxWEnTCV5mYYda2x9PCsI/VOLk9ZsLBhFqkQUMM/qY72/UQe0zhAuff0NztevN7KY9VHHBwbwIDAQAB")
	result, err := client.SystemOauthToken(context.TODO(), param)
	if err != nil {
		fmt.Println(err)
		return err
	}
	if user.AliOpenid != "" || result.OpenId == "" {
		return sferrors.NewStringf("非法操作")
	}
	var userNew model.My_user
	err = db.GetDb().Table("my_user").Where("ali_openid=?", result.OpenId).First(&userNew).Error
	if err == nil || userNew.Id > 0 {
		return sferrors.NewStringf("该支付宝已被其他账号绑定")
	}

	err = db.GetDb().Table("my_user").Where("id=?", user.Id).UpdateColumn("ali_openid", result.OpenId).Error
	if err != nil {
		return sferrors.New(err)
	}
	return nil
}

func CheckOpenid(authCode string, user model.My_user) error {

	appID := "2021005114691079"
	privateKey := "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8ol9RAQSO/Ee9eSXqU1iaos57YHMd951UEOYychwvSdQaQbTF1I6S5zvu5Jy1jksMlnb8sbLkQjC+afns3ln9EyQZLaVa6s+K2bZ7Y/s7AboeJmBP2opBuOL5Su9KWzMcfB5BsVrueASvpkxX6RhS/ohivnLOFmhEJVCjeCsb2r4BSAOXU4u954C+lVHrAw84mpw8CiLHHrnaZcT/gyOILAg8feN50qXPqoTF+lXWtnM5VNpvynoUfVoXZU9ECLk0X0JqKjfOUDSODwV8C0GFDSUkIfm/y3q6orZoWpJoBw9izUfBCIKOnjP0EBpm302fGjce/Ii0NoInnqU72HYLAgMBAAECggEADnmTQN8IH4TCaIq0Wx/cK7mUiKklDEak/v9w+qwA96v9p6olGg0uh51Q3wsR72ik36loJgnoxVVC8kmlkQSa+S2P1pLHHovoMypzs1I/xA5k3s1pFJebhmGOoTyb0iPfby8jDz1x0dzJ10S2lQRhBnVbt+APZTsY8ym9311oSXHErQyU3GLkB6jcFztuXrQE/cy6boXDFjYLMHK9EdevfWUiiglc3T3/mS/k/4soV6l2ZATFGf/QSzdHDHPcVUA8/nRgoHfgVw2CyT9uy4+cDimyHjZsKrZGhUN+eYF6GPGaA7I5jafwd+X7b6RQZTve1ZQpJuraWHHcnqVBiPMtQQKBgQD3spxknY0zA+WwcmENl67JEzacIRj6TNnIrfyItt8zbop11NGTSz+kOWgVK2L1ZNDWgLb78GzVwZhImrNYCdtRyoU+x3Ov58tLA1s5cQXcQ3ed0V2AyhOD81RJxx62A3BKL3hmaPZv6EYTZPht1nhM3f35gGJYn/kIyHx4QIYIKwKBgQDC9PaOsBPNbFUBNmPvGqcCgLpfvhT2y8LcLPmGKvmyCwpjXDl6JcZTiL6iVWdhHajUSN9gny+B3BrcdUSIC6wX6ngRvns8kKaEghaZ2e2kfdEst68fxJKwmraJskoVYoRlVOqhG7kv05NdAIVfNjyLf3GFFflDtpCZKUhYV5N5oQKBgQCOfrwlzdVbEdTjSwm2ThqJJnp2GzxJUki/gAgVhk6lQeR5oPLn75QvH69W7lzBmzvhCq86BLg3yGWY7xZZgWIgjYdV9FwDSC/VeZrTUsfyTQXVToqh+p57fFJzFw/92cCRjPmQwLV074TQx3d7iLQwVE01abwqC+2yNdQVQdlZfQKBgHTZPa3n+vZ0p75CS0NURiVJ4mrgEtCY8AHnMucneuErnN5QuGJtoaEPpLLHXV7n1/jkdH4E6wR6V98qqhigZlUvQyffcx2p1SQ2lDVCWRzuDCExNtVjjEgtcpq1MZH/qjrR2o8VfrkbpodneIjIiSFTEUcjFHNxSNmD1MieNNYBAoGAN205UpLBTZkm+fEx2Y6KikBH5aHcuVSQj/TsD+7qrZIfAMPmQPpBFGqpAx+dtG52THNyWUlVJ9N62cHV1HL65uNd2PTO3k5Rz4j5ib9XwFZtl2O4ESSy9oQN/RlseygSxJ3OSx8Pc4RIKlExjfjyripwx126M2ig/OsCt2K9u94="
	var client, err = alipay.New(appID, privateKey, true)
	if err != nil {
		fmt.Println(err)
		return err
	}
	param := alipay.SystemOauthToken{
		Code:      authCode,
		GrantType: "authorization_code",
	}
	client.LoadAliPayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjDdrjze7ztRp7Xv6OmOlkFSnPUoxGzi+bzRu1e90k8iACe2lZUFyLRRKoXmkgdulNDm8H2KKZPfj7y+48EkA99dHUeqFeUghX/geKOCEYxm6dZcJZlV/veCdVL3soCloTDqXeczdZrSr24vRVzVhzRyn7+dCJ9OBJiMqjZ8oN5/BnBqO+os8qn1eX8julFDc0K8FDJqqSEzNF9Ip8JzBFzdyluMq0eVjcECTxHuyPeDzZ7gzeqkk7ritY1mCg6nFVlwL6fBDq6Yl7NxWEnTCV5mYYda2x9PCsI/VOLk9ZsLBhFqkQUMM/qY72/UQe0zhAuff0NztevN7KY9VHHBwbwIDAQAB")
	result, err := client.SystemOauthToken(context.TODO(), param)
	if err != nil {
		fmt.Println(err)
		return err
	}
	if result.OpenId == "" || user.AliOpenid == "" {
		return sferrors.NewStringf("非法操作")
	}

	if user.AliOpenid != result.OpenId {
		return sferrors.NewStringf("必须是自己绑定的支付宝")
	}

	return nil
}
